const http = require('http');
const events = require('events');
const fs = require('fs');
const url = require('url');
var querystring = require('querystring');

const eventEmitter = new events.EventEmitter();

// 创建事件处理程序
let connectHandler = function connected() {
    console.log('连接成功。');
}

eventEmitter.on("conn", connectHandler);

http.createServer(function (request, response) {

    // HTTP 头部  status OK, 200 contentType
    response.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });

    let params = url.parse(request.url, true).query; // get 参数

    let body = "";
    request.on('data', function (chunk) {
        body += chunk;
    });
    request.on('end', function () {
        body = querystring.parse(body); //// post 参数
    });

    eventEmitter.emit("conn");

    response.end('Hello World!  黄义刚！\n');
}).listen(8888);


console.log('Server running at http://127.0.0.1:3000/');

// 全局捕获未捕获的Error
process.on('uncaughtException', function (err) {
    //打印出错误
    console.log(err);
    //打印出错误的调用栈方便调试
    // console.log(err.stack);
});